function generateparenthesis(n) {
  let result = []
  function dfs(left, right, n, str) {
    if (left === n && right === n) {
      return result.push(str)
    }
    if (left < n) {
      dfs(left + 1, right, n, str + '(')
    }
    if (right < left && right < n) {
      dfs(left, right + 1, n, str + ')')
    }
  }
  dfs(0, 0, n, '')

  return result
}

let r = generateparenthesis(3)
console.log(r)
